package com.zisu.aradvertising.service.impl;

import com.aliyuncs.mts.model.v20140618.QuerySmarttagJobResponse;
import com.zisu.aradvertising.pojo.JsonResult;
import com.zisu.aradvertising.pojo.ResultCode;
import com.zisu.aradvertising.pojo.Video;
import com.zisu.aradvertising.repository.DaoVideo;
import com.zisu.aradvertising.service.VideoService;

import com.zisu.aradvertising.utils.OSSUtil;
import com.zisu.aradvertising.utils.VideoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import static java.lang.Thread.sleep;

@Service
public class VideoImpl implements VideoService {
    @Autowired
    OSSUtil OSSUtil;
    @Autowired
    VideoUtil VideoUtil;

    @Override
    public JsonResult uploadVideo(MultipartFile videoFile) {
        String filePath = OSSUtil.uploadFile(videoFile, videoFile.getOriginalFilename(), 0);
        return new JsonResult(ResultCode.SUCCESS, filePath);
    }

    @Override
    public JsonResult analyzeVideo(MultipartFile videoFile) throws InterruptedException {
        String filePath = OSSUtil.uploadFile(videoFile, videoFile.getOriginalFilename(), 0);
        String JobId = VideoUtil.submitSmarttagJob(filePath,"");
        QuerySmarttagJobResponse response = VideoUtil.querySmarttagJob(JobId);
        int i = 1;
        while(!response.getJobStatus().equalsIgnoreCase("Success")){
            System.out.println(response.getJobStatus());
            System.out.println("查询"+i);
            i++;
            sleep(2000);
            response = VideoUtil.querySmarttagJob(JobId);
        }
        return new JsonResult(ResultCode.SUCCESS, response);
    }

    //实例化抽象类
    @Resource
    private DaoVideo daoVideo;
    //实现上传视频
    @Override
    public Video videouploadService(MultipartFile videoFile,MultipartFile pictureFile,int cid,String vname,String vstyle){
        Video m=new Video();
        String videoFilePath = OSSUtil.uploadFile(videoFile, videoFile.getOriginalFilename(), 0);
        String pictureFilePath = OSSUtil.uploadFile(pictureFile, pictureFile.getOriginalFilename(), 0);
        m.setFengmian(pictureFilePath);
        m.setVsource(videoFilePath);
        m.setCid(cid);
        m.setVstyle(vstyle);
        m.setVname(vname);
        Date date = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        try {
            m.setVreleasetime(df.parse(df.format(date)));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return daoVideo.save(m);
    }
    //实现编辑视频信息
    @Override
    public Video editService(int vid, String videorequirement, String videointroduction) {
        Video video = daoVideo.findByVid(vid);
        if(video !=null)
        {
            video.setVintroduction(videointroduction);
            video.setVrequirement(videorequirement);
            daoVideo.save(video);
        }
        else {
            return null;
        }
        return video;
    }

    @Override
    public List<Video> findallService() {
        return daoVideo.findAll();
    }
}
